#include <bits/stdc++.h>
using namespace std;
#define int long long

struct Point {
	int x, y;
} a[500005];

int n;
int pre[500005], suf[500005];
vector<int> edges[500005];
map<pair<int, int>, int> vis;
const int d[4][2] = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};
bool chk(int x, int y) {
	return (!!vis[{x, y}] + !!vis[{x, y + 1}] + !!vis[{x + 1, y}] + !!vis[{x + 1, y + 1}]) != 4;
}

bool cmp1(Point x, Point y) {
	return x.x < y.x;
}
bool cmp2(Point x, Point y) {
	return x.y < y.y;
}
int dst[500005], front, rear, q[500005], sz[500005], dep[500005], fa[500005];

inline void dfs(int i) {
	dst[1] += dep[i];
	for (auto j : edges[i]) {
		if (j == fa[i]) continue;
		fa[j] = i; dep[j] = dep[i] + 1;
		dfs(j);
		sz[i] += sz[j];
	}
	++sz[i];
}

inline void dfs2(int i, int &ans) {
	ans += dst[i];
	for (auto j : edges[i]) {
		if (j == fa[i]) continue;
		dst[j] = dst[i] + n - sz[j] * 2;
		dfs2(j, ans);
	}
}

signed main() {
	freopen("game.in", "r", stdin); freopen("game.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	bool ok = 1;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i].x >> a[i].y;
		vis[{a[i].x, a[i].y}] = i;
	}
	for (int i = 1; i <= n; ++i) {
		ok &= chk(a[i].x, a[i].y);
	}
	if (n <= 4000) {
		for (int i = 1; i <= n; ++i) {
			for (int k = 0; k < 4; ++k) {
				int nx = a[i].x + d[k][0], ny = a[i].y + d[k][1];
				if (vis[{nx, ny}]) {
					edges[i].push_back(vis[{nx, ny}]);
					edges[vis[{nx, ny}]].push_back(i);
				}
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; ++i) {
			q[front = rear = 1] = i;
			memset(dst, 255, sizeof(dst));
			dst[i] = 0;
			while (rear + 1 != front) {
				int fx = q[front++];
				for (auto j : edges[fx]) {
					if (dst[j] == -1) {
						dst[j] = dst[fx] + 1;
						q[++rear] = j;
					}
				}
			}
			for (int i = 1; i <= n; ++i) ans += dst[i];
		}
		cout << ans / 2 << "\n";
	} else if (ok) { // tree
		int tot = 0;
		for (int i = 1; i <= n; ++i) {
			for (int k = 0; k < 4; ++k) {
				int nx = a[i].x + d[k][0], ny = a[i].y + d[k][1];
				if (vis[{nx, ny}] && i < vis[{nx, ny}]) {
					++tot;
					edges[i].push_back(vis[{nx, ny}]);
					edges[vis[{nx, ny}]].push_back(i);
				}
			}
		}
		int ans = 0;
		dfs(1);
		dfs2(1, ans);
		cout << ans / 2 << "\n";
	} else { // idk
		int ans = 0;
		sort(a + 1, a + n + 1, cmp1);
		for (int i = 1; i <= n; ++i) {
			pre[i] = pre[i - 1] + a[i].x;
		}
		for (int i = n; i >= 1; --i) {
			suf[i] = suf[i + 1] + a[i].x;
		}
		for (int i = 1; i <= n; ++i) {
			ans += a[i].x * (i) - pre[i] + suf[i + 1] - a[i].x * (n - i);
		}
		sort(a + 1, a + n + 1, cmp2);
		for (int i = 1; i <= n; ++i) {
			pre[i] = pre[i - 1] + a[i].y;
		}
		for (int i = n; i >= 1; --i) {
			suf[i] = suf[i + 1] + a[i].y;
		}
		for (int i = 1; i <= n; ++i) {
			ans += a[i].y * (i) - pre[i] + suf[i + 1] - a[i].y * (n - i);
		}
		cout << ans /2 << "\n";
	}
	return 0;
}
